package com.chaopei.user_mng.service.impl;

import com.chaopei.user_mng.entity.UserInfo;
import com.chaopei.user_mng.mapper.UserInfoMapper;
import com.chaopei.user_mng.service.UserInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author Corday 天涯
 * @Date 2022/4/29 9:56
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     *
     * @param userName
     * @return
     */
    @Override
    public boolean findUserName(String userName) {
        Integer result = userInfoMapper.selectUserInfoByUserName(userName);
        return result != null;
    }

    /**
     *
     * @param userInfo
     * @return
     */
    @Override
    public String registerUserInfo(UserInfo userInfo) {
        //用户不存在则添加
        if (!findUserName(userInfo.getUserName())) {
//            拿到当前的日期
            Date date = new Date();
            userInfo.setRegDate(date);
            //先要处理用户信息的业务
            int number = userInfoMapper.insertUserInfo(userInfo);
            System.out.println("返回:"+number);
            return number > 0 ? "注册成功!" : "注册失败,系统有误!";
        }
        return "用户名已存在!";
    }

    /**
     *
     * @param userName
     * @param password
     * @return boolean
     */
    @Override
    public Integer userLogin(String userName, String password) {
        Integer temp = userInfoMapper.selectUserByUserNameAndPassword(userName, password);
        return temp;
    }
}
